package com.xmz.controller;


import cn.hutool.core.util.StrUtil;
import com.xmz.entity.Order;
import com.xmz.service.IOrderService;
import com.xmz.util.PageResult;
import com.xmz.util.Result;
import com.xmz.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderApi")
public class OrderController  {

    @Autowired
    private IOrderService orderService;
    @PostMapping("/loadList")
    public PageResult<Order> loadList( Integer pageNo,
                                       Integer pageSize,
                                      @RequestBody OrderVo orderVo){
        return orderService.loadList(pageNo,pageSize,orderVo);
    }

    @GetMapping("/getAllOrder")
    public List<Order> getAll(){
        return  orderService.getAllOrder();
    }

    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody Order order){
        int i = orderService.addOrder(order);
        if(i > 0) return  Result.success("添加成功！");
        return Result.error("添加失败！");
    }

    @DeleteMapping("/deleteOrder")
    public Result deleteBatchStudent(@RequestBody List<Integer> id){
        int i = orderService.deleteUser(id);
        if(i > 0) return Result.success("删除成功！");
        return Result.error("删除失败！");
    }
    @PutMapping("/updateOrder")
    public Result updateStudent( @Validated @RequestBody Order order ){
        if(!verifyId(order)) return Result.error("修改失败！");
        int i = orderService.updateUser(order);
        if(i > 0) return Result.success("修改成功！");
        return Result.error("修改失败！");
    }

    /**
     * 校验需要更新的订单信息的id是否为空
     * @param order
     * @return
     */
    private boolean verifyId(Order order){
        if(order.getId() != null && !StrUtil.isNotBlank(order.getId().toString()))
            return  true;
        return  false;
    }
}
